'use strict'

const amazonScraper = require('amazon-buddy');
const { Client } = require('@elastic/elasticsearch');
const client = new Client({ node: 'http://localhost:9200' });
const rp = require('request-promise');

const _keyword = ['N95 mask', 'ride on car for kids'];
const _countries = ['US', 'CA'];
const _number = 50;
const _interval = 30 * 60000;
const _index_name = 'products-spider-' + Date.now();

const proxyPool = {
  uri: 'http://127.0.0.1:5010/all',
  json: true // Automatically parses the JSON string in the response
};

const cb = (async () => {
  try {
    let res = await rp(proxyPool);
    let proxy = res.filter(x => !x.https).map(x => x.proxy);
    console.log('avaliable proxy:', proxy.length);
    if (!proxy || proxy.length == 0) {
      proxy = undefined;
      console.log('proxy service is not avaliable');
    }

    const now = new Date().toISOString();
    console.log(`start ${now}`);
    for (const cc of _countries) {
      console.log(`country: ${cc} --`)
      for (const kw of _keyword) {
        console.log(`keyword: ${kw} --`)
        const products = await amazonScraper.products({ proxy: proxy, keyword: kw, number: _number, country: cc, bulk: false });
        console.log(`found ${products.totalProducts} for '${kw}' in ${cc}`);

        for (const p of products.result) {
          await client.index({
            index: _index_name,
            body: {
              time: now,
              kw: kw,
              cc: cc,
              ...p
            }
          })
          console.log('inserting:', p.price?.current_price, p.title);
        }
      }
    };
    console.log('-- finished --');
  } catch (error) {
    console.log(error);
  } finally {
    setTimeout(cb, _interval + Math.floor(Math.random() * 60000));
  }
});

async function run() {
  try {
    const exists = await client.indices.exists({ index: _index_name });
    if (exists.body) {
      await client.indices.delete({ index: _index_name });
    }
    await client.indices.create({
      index: _index_name,
      body: {
        mappings:
        {
          properties: {
            "amazonChoice": {
              "type": "boolean"
            },
            "amazonPrime": {
              "type": "boolean"
            },
            "asin": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "bestSeller": {
              "type": "boolean"
            },
            "position": {
              "properties": {
                "global_position": {
                  "type": "long"
                },
                "page": {
                  "type": "long"
                },
                "position": {
                  "type": "long"
                }
              }
            },
            "price": {
              "properties": {
                "before_price": {
                  "type": "float"
                },
                "currency": {
                  "type": "text",
                  "fields": {
                    "keyword": {
                      "type": "keyword",
                      "ignore_above": 256
                    }
                  }
                },
                "current_price": {
                  "type": "float"
                },
                "discounted": {
                  "type": "boolean"
                },
                "savings_amount": {
                  "type": "float"
                },
                "savings_percent": {
                  "type": "float"
                }
              }
            },
            "reviews": {
              "properties": {
                "rating": {
                  "type": "float"
                },
                "total_reviews": {
                  "type": "long"
                }
              }
            },
            "score": {
              "type": "float"
            },
            "sponsored": {
              "type": "boolean"
            },
            "thumbnail": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "title": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "url": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        }

      }
    });

    await cb();
  } catch (err) {
    console.log(err);
  }
}

run();